import {message} from 'antd';

message.config({
    top: 100,
    maxCount: 3,
});

const toType = (obj) => Object.prototype.toString.call(obj);

const combineObj = function () {
    let target = {};
    for (let i = 1; i < arguments.length; i++) {
        let source = arguments[i];
        for (let key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
                target[key] = source[key];
            }
        }
    }
    return target;
};

const findId = (result, arr, name) => {
    if (!arr.length || toType(arr) !== '[object Array]' || !arr[0].id)
        return result;
    if (!arr[0][name] || !arr[0][name].length || toType(arr[0][name]) !== '[object Array]') {
        result.push(arr[0].id);
        return result
    } else {
        result.push(arr[0].id);
        return findId(result, arr[0][name], name);
    }
};

// 发送接口将空数据转化为null
const emptyStringToNull = (o) => {
    for (var key in o) {
        switch (true) {
            case toType(o[key]) === '[object String]':
                o[key].trim() === '' ? o[key] = null : o[key] = o[key].trim();
                break;
            case toType(o[key]) === '[object Object]':
                o[key] = emptyStringToNull(o[key]);
                break;
            case toType(o[key]) === '[object Array]':
                o[key] = emptyStringToNull(o[key]);
                break;
            default:
                break;
        }
    }
    return o;
}

export default {

    seekPath(obj) {
        let pathname = this.props.location.pathname;
        let isSome = !!obj.pathname ? obj.pathname === pathname : obj === pathname;
        if (isSome)
            return false;
        this.props.history.push(obj);
    },

    setCallback(obj = {}) {
        let cbs = Array.prototype.slice.call(arguments).slice(1);
        this.setState(obj, () => {
            if (!cbs.length)
                return false;
            cbs.map(cb => toType(cb) === '[object Function]' && cb.call(this))
            return false;
        })
    },

    tooltip: (options, ccb = null, afcb = null) => {
        let {level, content, duration} = options;
        message[level](content, duration || 2, ccb || '').then(afcb || '');
    },

    storage: (type = 'session', name = '', data = {}) => {
        if (!name)
            return;
        let storageData = JSON.stringify(data);
        type === 'session'
            ? window.sessionStorage.setItem(name, storageData)
            : window.localStorage.setItem(name, storageData);
    },

    getStorage: (type = 'session', name = '') => {
        if (!name)
            return;
        let data = type === 'session' ? window.sessionStorage.getItem(name) : window.localStorage.getItem(name);
        if (!data || !Object.keys(data))
            return {}
        return JSON.parse(data);
    },

    objectAssign: Object.assign || combineObj,

    // 递归获取 数组下第一个id 左侧树使用
    findFirstIds: (arr, name = 'subs') => {
        return findId([], arr, name);
    },

    transData: emptyStringToNull
}